﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webnew.Common;
using System.Net;
namespace webnew
{
    public partial class login : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                
                this.Title = "用户登录-" + webnew.Common.CommonHelper.GetSystemName();
                this.txtUid.Focus();
                
            }
            
        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {           
            Model.Sys_UserInfo info = new webnew.Model.Sys_UserInfo();
            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间
            
            string error = "";                   //获取错误信息
            try
            {
                if (txtUid.Text == string.Empty)
                {
                    Jscript.Alert(this, "请输入用户名！");
                    return;
                }
                else if (DataValid.CheckNumber(4, txtUid.Text.Trim()))
                {
                    Model.View_UserInfo ViewInfo = new webnew.Model.View_UserInfo();
                    ViewInfo = new BLL.View_User().GetStaffNOModel(txtUid.Text);
                    info = new BLL.Sys_User().GetLogModel(ViewInfo.Loginid);
                    if (ViewInfo.StaffNO != txtUid.Text)
                    {
                        Jscript.Alert(this, "不存在该员工编号！");
                        return;
                    }
                }
                else
                {
                    info = new BLL.Sys_User().GetLogModel(txtUid.Text.Trim());
                    if (info.Loginid != txtUid.Text)
                    {
                        Jscript.Alert(this, "不存在该用户名！");
                        return;
                    }
                }
                             
                 if (info.isdel == 2)
                {

                    Jscript.Alert(this, "此用户已被停用,请与系统管理员联系");
                    return;
                }
                 //else if (info.password != txtPwd.Text)
                 else if (info.password != Common.CommonHelper.MyMd5(txtPwd.Text))
                {
                    //对密码连续输入错误5次以上的用户进行锁定
                    ViewState["PassWordCount"] = Convert.ToInt32(ViewState["PassWordCount"]) + 1;
                    if (Convert.ToInt32(ViewState["PassWordCount"]) >= 5) new BLL.Sys_User().LockUser(info.Id);
                    //int iCount = 5 - Convert.ToInt32(ViewState["PassWordCount"]); 还剩下" + iCount + "次!
                    Jscript.Alert(this, "密码不正确,请重试!注:输入5次错误密码,系统将自动停用用户");
                    return;
                }
                 //新增用户初次登陆，强制修改密码
                if (info.updatePwdTime == DateTime.MinValue) Response.Redirect("ResetPassWord.aspx?MsgType=0"); 
                //实现三个月强制修改密码功能
                if (new BLL.Sys_User().ExistUpdatePwdTime(info.Id) == "1") Response.Redirect("ResetPassWord.aspx?MsgType=1");
                modelLog.UserID = info.Id;                  //用户ID
                //增加一条日志，如果error!=""，就增加一条失败记录；否则就增加一条成功的记录。
                new BLL.Sys_Log().AddSysLog(error, "登录", modelLog, "", "");        
            }
            catch (Exception errorLogin)
            {
               error = errorLogin.ToString();
               modelLog.UserID = info.Id;                  //用户ID
               //增加一条日志，如果error!=""，就增加一条失败记录；否则就增加一条成功的记录。
               new BLL.Sys_Log().AddSysLog(error, "登录", modelLog, "", "");
            }                   
           
            //将用户名和密码记录于客户端，Session超时则自动登入，IE关闭时自动清除。
           Response.Cookies["loginid"].Value = info.Loginid;
           //Response.Cookies["loginid"].Expires = DateTime.MinValue;
           Response.Cookies["pwd"].Value = info.password;
           //Response.Cookies["pwd"].Expires = DateTime.MinValue;
           //将用户实体共享
           Session["user"] = new webnew.BLL.View_User().GetModel(info.Id, info.StaffId);
          
           //更新登录时间
           new BLL.Sys_User().updateLoginDate(info.Id);
           info.remark = Dns.GetHostName();
           new BLL.Sys_User().updateremark(info.remark,info.Id);
           Jscript.WriteJs(this, "openwin()");
           
          // Response.Redirect("Default.aspx");
        }
    }
}
